﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace("SCS");

SCS.MessageBoxIcon = function () {

    throw Error.invalidOperation();
};

SCS.MessageBoxIcon.prototype = {
    None: 0,
    Error: 1,
    Question: 2,
    Warning: 3,
    Information: 4
};

SCS.MessageBoxIcon.registerEnum('SCS.MessageBoxIcon');

SCS.MessageBoxButtons = function () {

    throw Error.invalidOperation();
};

SCS.MessageBoxButtons.prototype = {
    OK: 0,
    OKCancel: 1,
    YesNoCancel: 2,
    YesNo: 3
};

SCS.MessageBoxButtons.registerEnum('SCS.MessageBoxButtons');

SCS.MessageBoxDefaultButton = function () {

    throw Error.invalidOperation();
};

SCS.MessageBoxDefaultButton.prototype = {
    Button1: 0,
    Button2: 1,
    Button3: 2
};

SCS.MessageBoxDefaultButton.registerEnum('SCS.MessageBoxDefaultButton');

SCS.DialogResult = function () {

    throw Error.invalidOperation();
};

SCS.DialogResult.prototype = {
    None: 0,
    OK: 1,
    Cancel: 2,
    Yes: 3,
    No: 4
};

SCS.DialogResult.registerEnum('SCS.DialogResult');

SCS.MessageBox = function (element) {
    SCS.MessageBox.initializeBase(this, [element]);

    /* Fields
    ****************************************************************************************************************/
    this._clickProxy;

    this._redirectUrl = null;
    this._icon = SCS.MessageBoxIcon.None;
    this._buttons = SCS.MessageBoxButtons.OK;
    this._defaultButton = SCS.MessageBoxDefaultButton.Button2;
    this._result = SCS.DialogResult.None;
    this._postBack = false;
    this._confirmMode = false;
    this._postBackScript = null;
    this._confirmBehavior = null;
    this._selectButtonNum = 0;

    this._icons = {};
};

SCS.MessageBox.prototype = {

    /* Setup
    ****************************************************************************************************************/

    initialize: function () {
        SCS.MessageBox.callBaseMethod(this, 'initialize');

        this._clickProxy = $.proxy(this._onClick, this);
        this._getButtons().bind("click", this._clickProxy);

        this._setButtons(this._buttons, this._defaultButton);
        this._setIcon(this._icon);

        if (this._confirmMode)
            this._enableCancel();

        this.add_propertyChanged(this.property_changed);
    },

    /* Teardown
    ****************************************************************************************************************/

    dispose: function () {

        this._getButtons().unbind("click");
        this._disableCancel();

        SCS.MessageBox.callBaseMethod(this, 'dispose');
    },

    /* Event Handlers
    ****************************************************************************************************************/

    _onClick: function (e) {

        var source = $(e.target),
            buttons = this._getButtons();

        this._setClickedState(e, buttons.index(source) + 1);
    },

    dialog_onEscape: function (e) {

        this.hide();
        this._setClickedState(e, -1);
    },

    property_changed: function (sender, args) {

        switch (args.get_propertyName().toUpperCase()) {

            case "CANCELMODE":

                if (this._cancelMode)
                    _enableCancel();
                else
                    _disableCancel();
                break;

            case "ICON":
                this._setIcon(this._icon);
                break;

            case "BUTTONS":
            case "DEFAULTBUTTON":
                this._setButtons(this._buttons, this._defaultButton);
                break;
        }
    },

    /* Public Methods
    ****************************************************************************************************************/

    show: function (text, caption, buttons, icon, defaultButton, redirectUrl) {

        //Sys.Debug.fail("");

        if (typeof (text) != "undefined")
            this.set_Text(text);

        if (typeof (buttons) != "undefined")
            this.set_Buttons(buttons);

        if (typeof (icon) != "undefined")
            this.set_Icon(icon);

        if (typeof (defaultButton) != "undefined")
            this.set_DefaultButton(defaultButton);

        if (typeof (redirectUrl) != "undefined")
            this.set_RedirectUrl(redirectUrl);

        SCS.MessageBox.callBaseMethod(this, 'show', [caption, undefined]);
    },

    /* Private Methods
    ****************************************************************************************************************/

    _enableCancel: function () {

        var props =
            {
                displayModalPopupID: this.get_ModalPopupBehavior().get_id(),
                postBackScript: this._postBackScript
            };

        this._confirmBehavior = $create(Sys.Extended.UI.ConfirmButtonBehavior, props, null, null, $get(this.get_targetControlId()));
    },
    _disableCancel: function () {

        if (this._confirmBehavior) {
            this._confirmBehavior.dispose();
            this._confirmBehavior = null;
        }
    },
    _getButtons: function () {

        return $(".buttons input:button", this.get_element());
    },

    _getIconImage: function () {

        return $(".icon img", this.get_element());
    },

    _getTextArea: function () {

        return $(".text", this.get_element());
    },

    _setClickedState: function (e, buttonNumber) {

        if (!this._postBack && !this._confirmMode)
            e.preventDefault();

        if (buttonNumber > -1)
            this._selectButtonNum = buttonNumber;

        var submit = false;

        switch (this._buttons) {

            case SCS.MessageBoxButtons.OK:
            case SCS.MessageBoxButtons.OKCancel:

                if (buttonNumber == 1) {
                    this._result = SCS.DialogResult.OK;
                    submit = true;
                }
                else
                    this._result = SCS.DialogResult.Cancel;

                break;

            case SCS.MessageBoxButtons.YesNo:
            case SCS.MessageBoxButtons.YesNoCancel:

                if (buttonNumber == 1) {
                    this._result = SCS.DialogResult.Yes;
                    submit = true;
                }
                else if (buttonNumber == 2)
                    this._result = SCS.DialogResult.No;

                else
                    this._result = SCS.DialogResult.Cancel;

                break;
        }

        if (this._confirmBehavior)
            this._confirmBehavior._handleConfirmDialogCompletion(submit);

        SCS.MessageBox.callBaseMethod(this, 'hide');

        if (this._redirectUrl && this._redirectUrl.length > 4)
            location.href = this._redirectUrl;
    },

    _setButtons: function (type, defaultButtonNum) {

        var buttons = this._getButtons(),
            defaultIndex = 1;

        buttons.hide();

        switch (type) {

            case SCS.MessageBoxButtons.OKCancel:
                $(buttons[0]).val("OK").show();
                $(buttons[1]).val("Cancel").show();
                break;
            case SCS.MessageBoxButtons.YesNoCancel:
                $(buttons[0]).val("Yes").show();
                $(buttons[1]).val("No").show();
                $(buttons[2]).val("Cancel").show();
                defaultIndex = 2;
                break;
            case SCS.MessageBoxButtons.YesNo:
                $(buttons[0]).val("Yes").show();
                $(buttons[1]).val("No").show();
                break;
            case SCS.MessageBoxButtons.RetryCancel:
                $(buttons[0]).val("Retry").show();
                $(buttons[1]).val("Cancel").show();
                break;
            default:
                $(buttons[0]).val("OK").show();
        }

        if (typeof (defaultButtonNum) != "undefined" && defaultButtonNum)
            defaultIndex = defaultButtonNum - 1;

        if (defaultIndex > 2 || defaultIndex < 0)
            defaultIndex = 1;

        try {
            buttons.get(defaultIndex).focus();
        }
        catch (e){}
    },

    _setIcon: function (type) {

        var url = null,
            icon = this._getIconImage();

        switch (type) {

            case SCS.MessageBoxIcon.Error:
                url = this._icons.Error;
                break;
            case SCS.MessageBoxIcon.Information:
                url = this._icons.Info;
                break;
            case SCS.MessageBoxIcon.Question:
                url = this._icons.Question;
                break;
            case SCS.MessageBoxIcon.Warning:
                url = this._icons.Warn;
                break;
        }

        if (url)
            icon.attr("src", url);
    },

    /* Properties
    ****************************************************************************************************************/

    get_Text: function () {

        return this._getTextArea().text();
    },
    set_Text: function (value) {

        if (this._getTextArea().text() != value) {
            this._getTextArea().text(value);
            this.raisePropertyChanged('Text');
        }
    },
    get_Buttons: function () {
        return this._buttons;
    },
    set_Buttons: function (value) {

        if (this._buttons != value) {
            this._buttons = value;
            this.raisePropertyChanged('Buttons');
        }
    },
    get_Icon: function () {
        return this._icon;
    },
    set_Icon: function (value) {

        if (this._icon != value) {
            this._icon = value;
            this.raisePropertyChanged('Buttons');
        }
    },
    get_DefaultButton: function () {
        return this._defaultButton;
    },
    set_DefaultButton: function (value) {

        if (this._defaultButton != value) {
            this._defaultButton = value;
            this.raisePropertyChanged('DefaultButton');
        }
    },
    get_PostBack: function () {
        return this._postBack;
    },
    set_PostBack: function (value) {

        if (this._postBack != value) {
            this._postBack = value;
            this.raisePropertyChanged('PostBack');
        }
    },
    get_ConfirmMode: function () {
        return this._confirmMode;
    },
    set_ConfirmMode: function (value) {
        this._confirmMode = value;
    },
    get_Result: function () {
        return this._result;
    },
    set_Result: function (value) {

        if (this._result != value) {
            this._result = value;
            this.raisePropertyChanged('Result');
        }
    },
    get_SelectedButtonNumber: function () {
        return this._selectButtonNum;
    },
    set_SelectedButtonNumber: function (value) {

        if (this._selectButtonNum != value) {
            this._selectButtonNum = value;
            this.raisePropertyChanged('SelectedButtonNumber');
        }
    },
    get_RedirectUrl: function () {
        return this._redirectUrl;
    },
    set_RedirectUrl: function (value) {

        if (this._redirectUrl != value) {
            this._redirectUrl = value;
            this.raisePropertyChanged('RedirectUrl');
        }
    },
    get_IconUrls: function () {
        return this._icons;
    },
    set_IconUrls: function (value) {
        this._icons = value;
    },
    get_postBackScript: function () {
        return this._postBackScript;
    },
    set_postBackScript: function (value) {
        this._postBackScript = value;
    }
};

/* Registration
****************************************************************************************************************/

SCS.MessageBox.registerClass('SCS.MessageBox', SCS.DialogControl);

